﻿using PagedList;
using PagedList.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GiaoTrinhEPU.Repositories.Repositories;
using GiaoTrinhEPU.Repositories.DataModels;
using GiaoTrinhEPU.Repositories;
using GiaoTrinhEPU.Repositories.Interfaces;
using GiaoTrinhEPU.Core.ActionResults;
using GiaoTrinhEPU.Core;

namespace GiaoTrinhEPU.Controllers
{
    public class LevelController : BaseController
    {
        /// <summary>
        /// Declare Level Repository
        /// </summary>
        private LevelRepository levelRepository;

        /// <summary>
        ///  Declare Session
        /// </summary>
        public static string SessionLevelSort = "SessionLevelSort";
        public static string SessionLevelSearch = "SessionLevelSearch";
        public static string SessionLevelSortOpt = "SessionLevelSortOpt";



        // GET: /Level/
        /// <summary>
        /// Constructor that allow to create a Comment controller
        /// </summary>
        public LevelController()
            : base()
        {
            this.levelRepository = new LevelRepository();
            ViewBag.SelectedMenuId = 25;
        }

        public ActionResult Index(int page = 1, int size = 25, String search = "", String opt = "", String sort = "")
        {
            ViewBag.Title = "Trình Độ";
            // Stored Session

            Session[SessionLevelSort] = sort.Trim().Length > 0 ? sort.ToLower() : Session[SessionLevelSort];
            Session[SessionLevelSortOpt] = opt.Trim().Length > 0 ? opt.ToLower() : Session[SessionLevelSortOpt];

            var level = this.levelRepository.ListLevels(false).ToList();

            // Search
            if (Session[SessionLevelSearch] != null)
            {
                ViewBag.Keyword = Session[SessionLevelSearch].ToString();

                level = level.Where(l => l.Name.ToUpper().Contains(ViewBag.Keyword.ToUpper())).ToList();

            }
            // Sort 
            ViewBag.SortNameOpt = "desc";
            if (Session[SessionLevelSort] != null)
            {
                switch (Session[SessionLevelSort].ToString())
                {
                    case "name":
                        level = Session[SessionLevelSortOpt].ToString().Equals("desc") ? level.OrderByDescending(s => s.Name).ToList() : level.OrderBy(s => s.Name).ToList();

                        Session[SessionLevelSort] = "name";
                        ViewBag.SortNameOpt = Sort.SetSortOrderOption(Session[SessionLevelSortOpt].ToString());
                        break;

                    default:
                        break;
                }
            }


            return View(level.ToPagedList(page, size));

        }
    }
}